package com.goddess.yeecolor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.tradeInfo;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.adapter.ProfessionTradeAdapter;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择职业和行业
 */
public class ProfessionTradeActivity extends BaseActivity implements View.OnClickListener{
    private List<tradeInfo>mList;
    private TextView mTitle;
    private Toolbar mToobar;
    private ProfessionTradeAdapter adapter;
    private String TAG;
    private View mView;
    private RecyclerView profession_recyclerview;
    private LinearLayoutManager ma;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_profession,null);
        setContentView(mView);
        TAG=getIntent().getBundleExtra("bundle").getString("Tag");
        initFindView();
        initData();
    }
    private com.goddess.yeecolor.view.dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    @Override
    public void initOnClck() {
        adapter.setOnClickListerner(new OnClickListener() {
            @Override
            public void OnClick(View view, int position) {
                tradeInfo info=mList.get(position);
                if (TAG.equals("行业")){
                    //通过判断是哪个界面跳过来的进行获取数据

                    //行业
                    Intent intent=new Intent();
                    intent.putExtra("name",info.getName());
                    setResult(2,intent);
                    setFiish(ProfessionTradeActivity.this);

                }
                else if (TAG.equals("职业")){

                    Intent intent=new Intent();
                    intent.putExtra("name",info.getName());
                    setResult(3,intent);
                    setFiish(ProfessionTradeActivity.this);
                }
            }

            @Override
            public void OnLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void initFindView() {
        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        mTitle= (TextView) findViewById(R.id.title_content);
        mTitle.setText(TAG);
//        setSupportActionBar(mToobar);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            setFiish(this);
        });
        profession_recyclerview= (RecyclerView) findViewById(R.id.profession_recyclerview);
        ma=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false);
        profession_recyclerview.setLayoutManager(ma);

    }

    @Override
    public void initData() {

        if (TAG.equals("行业")){
            //通过判断是哪个界面跳过来的进行获取数据

            setRequestTrade(MyUrl.url+MyUrl.trade,1);//行业

        }
        else if (TAG.equals("职业")){
            setRequestTrade(MyUrl.url+MyUrl.profession,2);//行业
        }

    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 请求接口
     */
    private void  setRequestTrade(String url,int tag){
        setProgress();
        Map<Object,Object>map=new HashMap<>();
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
    }

    private tradeInfo info;
    private String jsonString;
    private int code;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try{
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                mList=new ArrayList<>();
                if (msg.what==1){
                    code=object.getInt("code");
                    ToastUtils.toast(object.getString("msg"));
                    if (code==100){
                        JSONArray result=object.getJSONArray("result");
                        for (int i=0;i<result.length();i++){
                            JSONObject item=result.getJSONObject(i);
                            info=new tradeInfo();
                            info.setId(item.getInt("id"));
                            info.setSort(item.getInt("sort"));
                            info.setName(item.getString("name"));
                            info.setAllow(item.getInt("allow"));
                            info.setPid(item.getInt("pid"));
                            mList.add(info);
                        }
                        adapter=new ProfessionTradeAdapter(ProfessionTradeActivity.this,mList);
                        profession_recyclerview.setAdapter(adapter);
                        initOnClck();//点击事件

                    }
                }
                else if (msg.what==2){
                    code=object.getInt("code");
                    if (code==100){
                        JSONArray result=object.getJSONArray("result");
                        for (int i=0;i<result.length();i++){
                            JSONObject item=result.getJSONObject(i);
                            info=new tradeInfo();
                            info.setId(item.getInt("id"));
                            info.setSort(item.getInt("sort"));
                            info.setName(item.getString("name"));
                            info.setAllow(item.getInt("allow"));
                            info.setPid(item.getInt("pid"));
                            mList.add(info);
                        }
                        adapter=new ProfessionTradeAdapter(ProfessionTradeActivity.this,mList);
                        profession_recyclerview.setAdapter(adapter);
                        initOnClck();//点击事件
                    }
                }
                if (dialog_progress!=null&&dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }


    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog_progress!=null&&dialog_progress.isShowing()){
            dialog_progress.dismiss();
        }
    }
}
